import os
import numpy as np



def sampling(source_dir, target_dir, ratio):
    feat_list = os.listdir(source_dir)

    if not os.path.exists(target_dir):
        os.mkdir(target_dir)

    for feat_filename in feat_list:
        feat_path = os.path.join(source_dir, feat_filename)
        if os.path.isdir(feat_path):
            continue

        filename = os.path.splitext(feat_path)

        if not filename[-1] in [".npy",]:
            continue

        data = np.load(feat_path, allow_pickle=True)
        new_data = data[::ratio,:]

        new_path = os.path.join(target_dir, feat_filename)
        np.save(new_path, new_data)

if __name__ == '__main__':
    source_dir = "/home/whut/D/workspace/video_features/output/resnet/resnet50_s1"
    target_dir = "/home/whut/D/workspace/video_features/output/resnet/resnet50_s4"
    sampling(source_dir, target_dir, ratio=4)